﻿using Cfg.Attributes;
using Cfg.ConfigProviders;
using NUnit.Framework;

namespace Cfg.Test.Attributes
{
    [TestFixture]
    public class MapAttributeTest
    {
        public interface ITestConfig
        {
            string X { get; } 

            string Var { get; }

            IFooSection FooSection { get; }
        }

        public interface IFooSection
        {
            [Map("Bar")]
            string Bar { get; }
        }

        public class FakeProvider : IConfigFileProvider
        {
            public ConfigKeyValues GetValues()
            {
                return new ConfigKeyValues()
                    // global
                    .Add("x", "x")
                    .Add("var", "%bar%/var")
                    // foosection
                    .Add("bar", "baaar");
            }
        }

        [Test]
        public void MapFlatFileToSections()
        {
            var configFileProvider = new FakeProvider();
            AppSettings.Init<ITestConfig>(configFileProvider);

            var cfg = AppSettings.Get<ITestConfig>();

            Assert.AreEqual("x", cfg.X);
            Assert.AreEqual("baaar", cfg.FooSection.Bar);
        }

        [Test]
        public void UseMappedValuesAsVariable()
        {
            var configFileProvider = new FakeProvider();
            AppSettings.Init<ITestConfig>(configFileProvider);

            var cfg = AppSettings.Get<ITestConfig>();

            Assert.AreEqual("baaar/var", cfg.Var);
        }
    }
}
